import Vue from 'vue';
import Vuex from 'vuex';
import lodash from 'lodash';
import rsa from '@/utils/rsa';
import { login, sendVerifyCode } from '@/api/login';
import { loginSuccess } from '@/api/system/tbSysUser';
import { queryConfigList } from '@/api/system/tbSysConfig';
import { queryPermissionMenu } from '@/api/system/tbSysMenu';
import { queryUserRoleByType3 } from '@/api/system/tbSysUserRole';
Vue.use(Vuex)
const state = {
	token: '',
	userInfo: {},
	menuData: [],
	sendData: {},
	authButton: [],
	principalData: []
}
const getters = {}
const actions = {
	// 发送验证码
	async sendCodeRequest({ commit }, params) {
		let result = await sendVerifyCode(params).then(res => res.data);
		if (result.code === '0') {
			return true;
		}
	},

	//用户登陆
	async loginRequest({ commit, dispatch }, data) {
		let password = rsa.rsaPublicData(data.password);
		let params = { ...lodash.cloneDeep(data), password };
		let result = await login(params).then(res => {
			commit('SET_TOKEN', res.headers['authorization']);
			return res.data;
		});
		if (result.code === 0) {
			let datas = Vue.prototype.$detailData(result.data);
			if (!!datas.picId && datas.picId !== '-1') {
				datas['avatarImg'] = await dispatch('getFileUrl', datas.picId);
			}
			commit('SET_USER_INFO', datas);
			dispatch('getPermissionMenu');
			dispatch('getUserByRoleType');
			dispatch('getDepartList');
			loginSuccess();
			return Promise.resolve(result.data);
		} else {
			return Promise.reject(result.msg);
		}
	},

	//获取权限菜单
	async getPermissionMenu({ commit }) {
		let result = await queryPermissionMenu().then(res => res.data);
		if (result.code === '0') {
			let { menuToRouter } = require('@/utils/getRouter');
			let datas = !result.data ? {} : result.data[0];
			commit('SET_MENU_DATA', new menuToRouter(datas.menu));
			commit('SET_AUTH_BUTTON', datas.button);
			return datas;
		}
	},

	//获取站点配置
	async getSiteConfig({ commit }) {
		let result = await queryConfigList().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? [] : result.data;
			commit('SET_SITE_CONFIG', datas);
		} else {
			Promise.reject(new Error('faile'));
		}
	},

	//获取用户数据 - 通过角色类型
	async getUserByRoleType({ commit }) {
		let params = { page: 1, limit: 99999, };
		let result = await queryUserRoleByType3(params).then(res => res.data);
		if (result.code === '0') {
			let datas = result.data || [];
			let dataArr = datas.map(item => lodash.pick(item, ['id', 'userName', 'filed10']));
			commit('SET_PRINCIPAL_DATA', dataArr);
			return dataArr;
		} else {
			Promise.reject(new Error('faile'));
		}
	}
}
const mutations = {
	SET_TOKEN: (state, d) => {
		state.token = d;
		Vue.prototype.$session.set('token', d);
	},
	SET_USER_INFO: (state, d) => {
		state.userInfo = d;
		Vue.prototype.$local.set('userInfo', d);
	},
	SET_MENU_DATA: (state, d) => {
		state.menuData = d;
		Vue.prototype.$local.set('menuData', d);
	},
	SET_AUTH_BUTTON: (state, d) => {
		state.authButton = d;
		Vue.prototype.$local.set('authButton', d);
	},
	SET_VERITY_CODE: (state, d) => {
		state.sendData = d;
		Vue.prototype.$session.set('sendData', d);
	},
	SET_PRINCIPAL_DATA: (state, d) => {
		state.principalData = d;
		Vue.prototype.$local.set('principalData', d);
	}
}
export default { state, getters, actions, mutations }